package com.wantong.jianpu.viewmodels;

import android.app.Application;
import android.os.Bundle;

import androidx.annotation.NonNull;

import com.wantong.jianpu.activitys.CreateTourActivity;
import com.wantong.jianpu.activitys.TourChildDetailActivity;
import com.wantong.jianpu.adapters.TourListAdapter;
import com.wantong.jianpu.base.BaseResultBean;
import com.wantong.jianpu.beans.DeviceListBean;
import com.wantong.jianpu.beans.InspectionDetailBean;
import com.wantong.jianpu.event.TourDetailEvent;
import com.wantong.jianpu.models.DeviceModel;
import com.wantong.jianpu.net.BaseObserver;
import com.wantong.jianpu.utils.Constants;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * @author PACY
 * @date 2022/5/6
 * description:
 */
public class TourDetailViewModel extends BaseViewModel<DeviceModel> {

    public DeviceModel model;
    public String id;
    public TourListAdapter mAdapter;
    public SingleLiveEvent<Integer> nowPosition=new SingleLiveEvent<>();
    public SingleLiveEvent<InspectionDetailBean> detail=new SingleLiveEvent<>();

    public SingleLiveEvent<Integer> currentNum=new SingleLiveEvent<>();//当前次数
    public SingleLiveEvent<Integer> status=new SingleLiveEvent<>();//当前状态


    public SingleLiveEvent<Boolean> completeEvent=new SingleLiveEvent<>();//完成本次巡检弹窗
    public TourDetailViewModel(@NonNull Application application) {
        super(application);
        model=new DeviceModel();
    }

    @Override
    protected void init() {
        super.init();
        initAdapter();
        getDeviceDetail();
    }

    private void initAdapter(){
        mAdapter=new TourListAdapter();
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            Bundle bundle=new Bundle();
            bundle.putSerializable("data",mAdapter.getItem(position));
            startActivity(TourChildDetailActivity.class,bundle);
        });
    }

    /**
     * 巡检
     */
    public BindingCommand toTourPage=new BindingCommand(() -> {
        Bundle bundle=new Bundle();
        bundle.putString("id",id);
        bundle.putInt("currentNum",currentNum.getValue());
        startActivity(CreateTourActivity.class,bundle);
    });

    /**
     * 完成本次巡检
     */
    public BindingCommand complete=new BindingCommand(() -> {
        completeEvent.setValue(true);
    });

    /**
     * 设置数据
     */
    public void setAdapterData(){
        List<InspectionDetailBean.RecordListDTO> data=new ArrayList<>();

        if(detail.getValue().getRecordList()!=null){
            for (InspectionDetailBean.RecordListDTO inspectionEquListDTO : detail.getValue().getRecordList()) {
                if(inspectionEquListDTO.getCurrentNum()==nowPosition.getValue()){
                    data.add(inspectionEquListDTO);
                }
            }
            mAdapter.setNewInstance(data);
        }
    }

    /**
     * 获取设备详情
     */
    public void getDeviceDetail(){
        showDialog();
        model.getDeviceDetail(id, new BaseObserver<InspectionDetailBean>() {
            @Override
            public void onSuccess(BaseResultBean<InspectionDetailBean> t) {
                detail.setValue(t.getData());
                status.setValue(t.getData().getStatus());
                currentNum.setValue(t.getData().getCurrentNum());
                nowPosition.setValue(t.getData().getCurrentNum());
                setAdapterData();
            }

            @Override
            public void onComplete() {
                super.onComplete();
                dismissDialog();
            }
        },getLifecycleProvider());
    }

    /**
     * 完成本次巡检
     */
    public void completeInspection(){
        showDialog();
        Map<String,Object> map=new HashMap<>();
        map.put("inspectionId",id);
        model.completeInspection(id, new BaseObserver() {
            @Override
            public void onSuccess(BaseResultBean t) {
                ToastUtils.show("提交成功");
//                EventBus.getDefault().post(new TourDetailEvent());
                getDeviceDetail();
            }

            @Override
            public void onComplete() {
                super.onComplete();
                dismissDialog();
            }
        },getLifecycleProvider());
    }
}
